package com.ruoyi.active.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.active.api.RemoteCfOrderService;
import com.ruoyi.active.api.domain.CfDrawal;
import com.ruoyi.active.api.domain.CfOrder;
import com.ruoyi.common.core.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.active.mapper.CfShopMapper;
import com.ruoyi.active.domain.CfShop;
import com.ruoyi.active.service.ICfShopService;
import org.springframework.util.DigestUtils;
import org.springframework.util.MultiValueMap;

/**
 * 商户管理Service业务层处理
 * 
 * @author zhangbiao
 * @date 2024-06-04
 */
@Service
public class CfShopServiceImpl implements ICfShopService 
{
    @Autowired
    private CfShopMapper cfShopMapper;

    @Autowired
    private RemoteCfOrderService orderService;

    /**
     * 查询商户管理
     * 
     * @param id 商户管理主键
     * @return 商户管理
     */
    @Override
    public CfShop selectCfShopById(Long id)
    {
        return cfShopMapper.selectCfShopById(id);
    }

    /**
     * 查询商户管理列表
     * 
     * @param cfShop 商户管理
     * @return 商户管理
     */
    @Override
    public List<CfShop> selectCfShopList(CfShop cfShop)
    {
        return cfShopMapper.selectCfShopList(cfShop);
    }

    /**
     * 新增商户管理
     * 
     * @param cfShop 商户管理
     * @return 结果
     */
    @Override
    public int insertCfShop(CfShop cfShop)
    {
        cfShop.setCreateTime(DateUtils.getNowDate());
        return cfShopMapper.insertCfShop(cfShop);
    }

    /**
     * 修改商户管理
     * 
     * @param cfShop 商户管理
     * @return 结果
     */
    @Override
    public int updateCfShop(CfShop cfShop)
    {
        return cfShopMapper.updateCfShop(cfShop);
    }

    /**
     * 批量删除商户管理
     * 
     * @param ids 需要删除的商户管理主键
     * @return 结果
     */
    @Override
    public int deleteCfShopByIds(Long[] ids)
    {
        return cfShopMapper.deleteCfShopByIds(ids);
    }

    /**
     * 删除商户管理信息
     * 
     * @param id 商户管理主键
     * @return 结果
     */
    @Override
    public int deleteCfShopById(Long id)
    {
        return cfShopMapper.deleteCfShopById(id);
    }

    @Override
    public Object shopLogin(CfShop cfShop) {
        CfShop shop = cfShopMapper.selectCfShopList(cfShop).get(0);
        if (shop==null){
            return "该用户不存在";
        }else {
            String password = DigestUtils.md5DigestAsHex(cfShop.getPassword().getBytes());
            System.out.println("你的密码："+password);
            System.out.println("正确密码："+shop.getPassword());
            boolean flag = password.equals(shop.getPassword());
            if (flag){
                return shop;
            }else {
                return "密码错误";
            }
        }
    }

    @Override
    public int shopRegister(CfShop cfShop) {
        cfShop.setPassword(DigestUtils.md5DigestAsHex(cfShop.getPassword().getBytes()));
        return insertCfShop(cfShop);
    }

    @Override
    public int hexiao(CfOrder cfOrder) {
        CfShop newShop = cfShopMapper.selectCfShopById(cfOrder.getShopId());
        Double money = newShop.getMoney()+cfOrder.getRealPrice();
        newShop.setMoney(money);
        cfShopMapper.updateCfShop(newShop);
        cfOrder.setState(2l);
        cfOrder.setVerificationTime(new Date());
        orderService.edit(cfOrder);
        return 1;
    }

    @Override
    public int tixian(CfDrawal cfDrawal) {
        CfShop newShop = cfShopMapper.selectCfShopById(cfDrawal.getCfShopId());
        Double money = newShop.getMoney()-cfDrawal.getMoney();
        newShop.setMoney(money);
        cfShopMapper.updateCfShop(newShop);
        orderService.add(cfDrawal);
        return 1;
    }
}
